// pages/aticles/articles.js


import ArticleAC from '../../js/article_access'
var util = require('../../utils/util.js');

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navItem : {
      newNav : 'top-nav-item-current',
      hotNav : '',
      recommendNav : ''
    },
    // 1 最新，2 热门， 3 推荐
    navArticleType : 1,
    albumsStatus : 0,
    //页面是否加载
    loadCache : {}
  },
  setNavCurrent : function(v) {
    var navItem = this.data.navItem;
    for(var i in navItem) {
      navItem[i] = '';
    }
    navItem[v] = 'top-nav-item-current';
    this.setData({
      navItem : navItem
    });

  }, 
  //加载文章列表，分参数，最新，热门，推荐，
  loadArticleList: function (p, cb) {
    var thisObj = p.thisObj;
    //切换列表
    thisObj.setData({ albumsStatus : 0});

    //已加载
    var cacheList = thisObj.data.loadCache[thisObj.data.navArticleType];
    if (cacheList) {
      if(!p.force) {//没有强制刷新
        if(cb) {
          cb();
        }
        //缓存加载
        thisObj.setData({ articleList: cacheList });
        return;
      }
    }

    wx.showLoading();

    ArticleAC.getArticles({
      maxLen : 340,
      type : this.data.navArticleType,
      pageSize : 30
    }, function (res) {
      console.log(res);
      if (res) {
        var list = res.list || [];
        var homeList = [];
        for (var i in list) {
          var obj = list[i];
          var content = obj.content || '';

          homeList.push({
            id: obj.id,
            title: obj.title,
            dtime:
              //obj.updateTimeString

              '',
            info: content,
            viewCount: obj.viewCount,
            tags: obj.tags,
            articleImg: (obj.thumbnailImgUrl && obj.thumbnailImgUrl + '_200X120' || app.globalData.defaultLogoImg)
          });
        }

        thisObj.setData({ articleList: homeList });

        thisObj.data.loadCache[thisObj.data.navArticleType] = homeList;
      }

      wx.hideLoading();

      if (cb) {
        cb();
      }
    });
  },
  clickNew: function (options) {
    this.setNavCurrent('newNav');
    this.setData({ navArticleType:1});
    this.loadArticleList({thisObj : this});
  },
  clickHot: function (options) {
    this.setNavCurrent('hotNav');
    this.setData({ navArticleType: 2 });
    this.loadArticleList({ thisObj: this });
  },
  clickRecommend: function (options) {
    this.setNavCurrent('recommendNav');
    this.setData({ navArticleType: 3 });
    this.loadArticleList({ thisObj: this });
  },
  clickAlbums: function (options) {
    this.setNavCurrent('albumsNav');
    this.setData({ albumsStatus: 1 });
    this.loadAlbums(this);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onload', options);
    this.setData(
      {
        barStatus: app.globalData.statusBar
      }
    );
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('on ready');
    this.clickNew();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('on unload');
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh');
    if (this.data.albumsStatus == 1) {
      /** */
      this.loadAlbums(this, function () {
        var t = setTimeout(function() {

          wx.stopPullDownRefresh();

          clearTimeout (t);
        }, 500);
      });
      /** */
    } else {
      this.loadArticleList({ thisObj: this, force: true }, function () {

        wx.stopPullDownRefresh();
      });
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('onReachBottom');
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /*** 详情跳转 */
  clickListItem: function (event) {
    
    util.clickListItemAndJump(event);
  },

  clickSearchInput: function (e) {
    app.clickSearchInput(e);
  }, 
  
  loadAlbums: function (thisObj, cb) {

    wx.showLoading();
    ArticleAC.getAlbums({
      viewDefunct : 0
    }, function(res) {
      console.log(res);
      if (res) {
        var albums = res;

        if (albums) {
          for (var i in albums) {
            var album = albums[i];
            album.logo = album.logo + '_140X200';
          }
        }

        thisObj.setData({ albumList: albums });
      }
      wx.hideLoading();

      if (cb) {
        cb();
      }
    });
  },
  /*** 专辑详情跳转 */
  clickAlbumItem: function (event) {
    util.clickAlbumItemAndJump(event);
  }
})